<template>
  <div>
    <!-- 面包屑 ------------------------------->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 --------------------------------->
    <el-card>
      <!-- 栅格布局 -->
      <el-row :gutter="20">
        <!-- 第一列 -->
        <el-col :span="8">
          <!-- 搜索框 -->
          <el-input
            placeholder="请输入内容"
            @clear="getGoodsList"
            v-model="queryData.query"
            @change="getGoodsList"
            clearable
          >
            <!-- 搜过按钮 -->
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <!-- 第二列 -->
        <el-col :span="4">
          <el-button type="primary" @click="$router.push('/goods/add')"
            >新增商品</el-button
          >
        </el-col>
      </el-row>
    </el-card>

    <!-- 你好 -->
    <el-table :data="goodsList" style="width: 100%" border stripe>
      <el-table-column label="#" type="index" width="50"></el-table-column>
      <el-table-column prop="goods_name" label="商品名称"></el-table-column>
      <el-table-column
        prop="goods_price"
        label="商品价格(元)"
      ></el-table-column>
      <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
      <!-- 状态列 -->
      <el-table-column label="创建时间">
        <template slot-scope="{ row }">
          {{ row.add_time | dataFormat }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template>
          <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>

          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 页码条 -->
    <el-pagination
      @size-change="pageSizeChange"
      @current-change="pageChange"
      :current-page="queryData.pagenum"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="queryData.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
export default {
  data () {
    return {
      queryData: {
        query: '', // 查询参数
        pagenum: 1, // 当前页码
        pagesize: 20 // 页容量
      },
      goodsList: [], // 用户数组
      total: 0 // 总用户数
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList () {
      // 发送异步请求，并 解构返回对象中的 data属性，赋值给 resBody
      const { data: resBody } = await this.$http.get('goods', {
        params: this.queryData
      })
      console.log(resBody)
      // 如果失败，则显示错误消息，并退出函数执行
      if (resBody.meta.status !== 200) return this.$msg.error(resBody.meta.msg)
      // 如果成功，则将返回的 分页数据 设置给 组件的 data，触发 视图的重绘
      this.goodsList = resBody.data.goods
      // 将 返回的 总用户数量 设置给 data
      this.total = resBody.data.total
    },
    pageSizeChange (newPageSize) {
      // 将 选择 的 新的 页容量 ，设置给 data
      this.queryData.pagesize = newPageSize
      // 重新请求 当前页数据
      this.getGoodsList()
    },
    pageChange (newPageIndex) {
      // 将 被点击的 页码 设置给 data
      this.queryData.pagenum = newPageIndex
      // 重新请求 当前页数据
      this.getGoodsList()
    }
  }
}
</script>

<style lang="less" scoped>
</style>
